/*
 * circle_queue.c
 *
 *  Created on: 2020��1��5��
 *      Author: Administrator
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "../../include/queue/circle_queue.h"

CircleQueue* init_circle_queue() {

	CircleQueue* queue = (CircleQueue*)malloc(sizeof(CircleQueue));
	queue->front = 0;
	queue->rear = 0;

	return queue;
}

void create_circle_queue(CircleQueue* queue, int array[], int length) {

	if (!is_empty_circle_queue(queue)) {
		printf("queue is not empty!");
		return;
	}

	for (int i = 0; i < length; ++i) {
		circle_enqueue(queue, array[i]);
	}
}

bool is_empty_circle_queue(CircleQueue* queue) {

	if (queue->front == queue->rear) {
		return true;
	}
	return false;
}

bool is_full_circle_queue(CircleQueue* queue) {

	if ((queue->rear + 1) % QUEUE_SIZE == queue->front) {
		return true;
	}
	return false;
}

bool circle_enqueue(CircleQueue* queue, ElemType value) {

	if (is_full_circle_queue(queue)) {
		printf("circle queue is full!");
		return false;
	}

	queue->elem[queue->rear] = value;
	queue->rear = (queue->rear+1) % QUEUE_SIZE;
	return true;
}

ElemType circle_dequeue(CircleQueue* queue) {

	if (is_empty_circle_queue(queue)) {
		printf("circle queue is empty!");
		return false;
	}

	ElemType temp = queue->elem[queue->front];
	queue->front = (queue->front + 1) % QUEUE_SIZE;
	return temp;
}

void print_circle_queue(CircleQueue* queue) {

	int p = queue->front;
	while (p != queue->rear) {
		printf("%d ", queue->elem[p]);
		p = (p + 1) % QUEUE_SIZE;
	}
	printf("\n");
}
